package com.handpay.nocardpay.baselibrary.smartrepayment;

import android.os.Bundle;
import android.view.View;
import android.webkit.WebSettings;
import android.webkit.WebView;
import android.webkit.WebViewClient;

import com.handpay.nocardpay.baselibrary.R;
import com.handpay.nocardpay.baselibrary.commbase.BaseProtocolKey;
import com.handpay.nocardpay.baselibrary.commbase.view.fragment.BaseFragment;
import com.handpay.nocardpay.baselibrary.config.SecureManager;
import com.handpay.nocardpay.baselibrary.logutils.HPLog;
import com.handpay.nocardpay.baselibrary.main.mine.model.bean.MerchInfo;
import com.handpay.nocardpay.baselibrary.main.mine.presenter.MinePresenterImpl;
import com.handpay.nocardpay.baselibrary.main.mine.view.IMerchInfoView;
import com.handpay.nocardpay.baselibrary.utils.AccountUtils;
import com.handpay.nocardpay.baselibrary.utils.ClientEngine;

import java.util.HashMap;

/**
 * Created by sxshi on 2017-12-11.
 */

public class SmartRePayFragment extends BaseFragment implements IMerchInfoView {
    private WebView mWebView;
    //获取商户信息
    private MinePresenterImpl minePresenter;
    public static String newBankFlag = "1";
    public static final String URL_KEY = "URL_KEY";
    private  String mHost;
    public static SmartRePayFragment newInstance(String url_key) {
        Bundle args = new Bundle();
        args.putString(URL_KEY, url_key);
        SmartRePayFragment fragment = new SmartRePayFragment();
        fragment.setArguments(args);
        return fragment;
    }

    @Override
    public int getLayoutId() {
        return R.layout.fragment_smartpay;
    }

    @Override
    public void initView(View view) {
        mWebView = (WebView) view.findViewById(R.id.wv_content);
        // 开启 localStorage
        mWebView.getSettings().setDomStorageEnabled(true);
        // 设置支持javascript
        mWebView.getSettings().setJavaScriptEnabled(true);
        // 启动缓存
        mWebView.getSettings().setAppCacheEnabled(true);
        // 设置缓存模式
        mWebView.getSettings().setCacheMode(WebSettings.LOAD_DEFAULT);
        //使用自定义的WebViewClient
        mWebView.setWebViewClient(new WebViewClient() {
            //覆盖shouldOverrideUrlLoading 方法
            @Override
            public boolean shouldOverrideUrlLoading(WebView view, String url) {
                if (url.contains("closewindow")) {
                    pop();
                } else {
                    view.loadUrl(url);
                }
                return true;
            }
        });


        minePresenter = new MinePresenterImpl(_mActivity);
        minePresenter.setiMerchInfoView(this);
    }

    @Override
    public void initListener() {

    }


    @Override
    public void initData(Bundle savedInstanceState) {
        mHost  =getArguments().getString(URL_KEY);

        queryMerchInfo();
    }

    /**
     * 获取当前用户信息
     */
    public void queryMerchInfo() {
//        HPLog.d(HPLog.SSX_TAG, "------------------查询商户信息请求-------------------");
        HPLog.d(HPLog.SSX_TAG, "accountName:" + AccountUtils.getAccountName(_mActivity));
        HashMap<String, Object> params = new HashMap<>();
        params.put(BaseProtocolKey.ACCOUNT, SecureManager.getInstance().des(AccountUtils.getAccountName(_mActivity), 1, null));
        params.put(BaseProtocolKey.CHANNEL, ClientEngine.getInstance().CHANNEL);
        params.put(BaseProtocolKey.NEWBANKFLAG, newBankFlag);
        if (minePresenter != null) {
            minePresenter.getMerchInfo(params, MerchInfo.class, true);
        }
    }

    @Override
    public void querySuccess(MerchInfo merchInfo) {
//        HPLog.d(HPLog.SSX_TAG, "------------------MainFragment查询商户信息返回 start-------------------");
        if (merchInfo == null) return;
        String name = SecureManager.getInstance().des(merchInfo.getName(), 0, null);
        String bankName = SecureManager.getInstance().des(merchInfo.getBankName(), 0, null);
        String cardNum = SecureManager.getInstance().des(merchInfo.getCardNum(), 0, null);
        String mSubBankName = SecureManager.getInstance().des(merchInfo.getSubBranchName(), 0, null);
        String certNo = SecureManager.getInstance().des(merchInfo.getCertNo(), 0, null);
        String mSubBankCode = merchInfo.getSubBankCode();//服务器未加密
        merchInfo.setName(name);
        merchInfo.setBankName(bankName);
        merchInfo.setCardNum(cardNum);
        merchInfo.setSubBranchName(mSubBankName);
        merchInfo.setSubBankCode(mSubBankCode);
        AccountUtils.saveUserName(_mActivity, name);
        AccountUtils.setAccountStatus(_mActivity, AccountUtils.convertStatus(merchInfo.getStatus()));
        String phone = SecureManager.getInstance().des(merchInfo.getPhone(), 0, null);
        AccountUtils.setAccountPhone(_mActivity, phone);
        AccountUtils.saveUsercertNo(_mActivity, certNo);
//        HPLog.d(HPLog.SSX_TAG, "------------------MainFragment查询商户信息返回end-------------------");
        String url = SmartRePayment.getSmartPayUrl(mHost,AccountUtils.getUserName(_mActivity), AccountUtils.getAccountPhone(_mActivity), AccountUtils.getUsercertNo(_mActivity), ClientEngine.getInstance().CHANNEL, AccountUtils.getMerchantCode(_mActivity));
        mWebView.loadUrl(url);

    }
}
